<?php
defined('IN_APPS') or die('Go away...');

require_once MODEL_PATH . DS . 'blog_model.php';
require_once MODEL_PATH . DS . 'comment_model.php';


class Blog extends Controller {
	
	public function index()
	{
		global $view;
		
		
		if (isset($_POST['type'])) {
			$type = $_POST['type'];
		}
		else {
			$type = 'all';
		}
		
		$view->assign('title', 'My Blog Posts');
		
		
		//create BlogModel instace
		$blogModel = new BlogModel();
		
		// get blog posts
		//$posts = $blogModel->getPublished();
		
		
		switch ($type)
		{
			case 'all':
				$posts = $blogModel->getAll();
				break;
			
			case 'published':
				$posts = $blogModel->getPublished();
				break;
			
			case 'unpublished':
				$posts = $blogModel->getUnpublished();
				break;
		}
		
		// assign to view
		$view->assign('blogs', $posts);
		
		
		$view->display('blog.index.php');
	}
	
	public function add()
	{
		global $view;
		
		$view->assign('title', 'Add New Blog Post');
		
		$view->display('blog.add.php');
	}

	
	public function create()
	{
		$security = new Security();
		
		$data['title']		= $security->xss_clean($_POST['title']);
		$data['content']	= $security->xss_clean($_POST['content']);
		$data['published']	= $security->xss_clean($_POST['published']);
		
		// check title
		if (empty($data['title']) OR $data['title'] == '') {
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'add'
					), 'Title cannot empty!', 'error');
		}
		
		// check content
		if (empty($data['content']) OR $data['content'] == '') {
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'add'
					), 'Content cannot empty!', 'error');
			
		}
		
		$data['created']	= date('Y-m-d H:i:s');
		//$data['user_id']	= $_COOKIE['user_id'];
		$data['user_id']	= $_SESSION['user_id'];
		
		// create BlogModel instance
		$blogModel = new BlogModel();
		
		// save operation
		if ($blogModel->create($data)) {
			// success
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'index'
					), 'Blog saved!', 'notice');
		}
		else {
			// error
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'add'
					), 'Cannot save blog post!', 'error');
		}
	}
	
	
	public function edit($id)
	{
		global $view;
		
		$blogModel = new BlogModel();
		
		// get single record, based on $id
		$blog = $blogModel->get($id);
		
		$blog['title'] = stripslashes($blog['title']);
		
		// assign to view
		$view->assign('blog', $blog);
		
		$view->assign('title', 'Edit Blog Post');
		$view->display('blog.edit.php');
	}
	
	/**
	 * Update exiting blog post
	 */
	public function update()
	{
		$security = new Security();
		
		$data['title']		= $security->xss_clean($_POST['title']);
		$data['content']	= $security->xss_clean($_POST['content']);
		$data['published']	= $security->xss_clean($_POST['published']);
		//$data['user_id']	= $_COOKIE['user_id'];
		$data['user_id']	= $_SESSION['user_id'];
		
		$id					= $security->xss_clean($_POST['id']);
		
		if (empty($data['title']) OR $data['title'] == '') {
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'edit'
					), 'Title cannot empty!', 'error');
		}
		
		if (empty($data['content']) OR $data['content'] == '') {
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'edit'
					), 'Content cannot empty', 'error');
		}
		
		$blogModel = new BlogModel();
		
		if ($blogModel->update($id, $data)) {
			// success
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'index'
					), 'Blog updated!');
		}
		else {
			// error
			redirect(
					array(
						'controller'	=> 'blog',
						'action'		=> 'index'
					), 'Blog cannot be updated!', 'error');
		}
	}
	
	
	
	public function view($id)
	{
		global $view;
		
		$commentModel = new CommentModel();
		
		// check if POST operation exists
		if ( isset( $_POST['c_title'] ) ) {
			
			// if title is empty
			if ( $_POST['c_title'] == '' OR empty( $_POST['c_title'] ) ) {
				redirect(
						array(
							'controller'	=> 'blog',
							'action'		=> 'view'
						), 'Comment title is required!', 'error');
			}
			
			// check if comment is empty
			if ( $_POST['c_comment'] == '' OR empty( $_POST['c_comment'] ) ) {
				redirect(
						array(
							'controller'	=> 'blog',
							'action'		=> 'view'
						), 'Comment message is required!', 'error');
			}
			
			$security = new Security();
			
			$data['c_title']		= $security->xss_clean($_POST['c_title']);
			$data['c_comment']		= $security->xss_clean($_POST['c_comment']);
			$data['c_created']		= date('Y-m-d H:i:s');
			$data['c_published']	= 1;
			$data['c_blog_id']		= $id;
			$data['c_user_id']		= $_SESSION['user_id'];

			$commentModel->create($data);	
		}
		
		// load all comment
		$comments = $commentModel->getBlogComment($id);
		
		for ($x = 0; $x < count($comments); $x++) {
			$comments[$x]['c_title']	= stripslashes($comments[$x]['c_title']);
			$comments[$x]['c_comment']	= stripslashes($comments[$x]['c_comment']);
		}
		
		// assign to view
		$view->assign('comments', $comments);
		
		// create instance
		$blogModel = new BlogModel();
		
		// load blog detail from database
		$blog = $blogModel->get($id);
		
		$blog['title']		= stripslashes($blog['title']);
		$blog['content']	= stripslashes($blog['content']);
		
		// assign blog content to view
		$view->assign('blog', $blog);
		
		// load view file
		$view->display('blog.view.php');
		
	}
	
}